<template>
    <!-- 列表页面 -->
    <div class="tableSample" v-if="!showAdd && !showEdit">
        <div class="header">
            <div class="header-left">
                <p class="title">开放城市
                    <el-button type="primary" plain style="margin-left: 30px;" @click="handleAdd">添加</el-button>
                </p>
            </div>
            <div class="header-right">
                <lin-search @query="onQueryChange" placeholder="请输入名称"/>
                <el-dropdown size="medium" style="margin: 0 10px;" @command="handleCommand" v-auth="'查询'">
                    <el-button size="medium">
                        {{searchProvince ? searchProvince : '选择省份'}}
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="['选择省份']">选择省份</el-dropdown-item>
                        <el-dropdown-item
                                v-for="status in provinceArr"
                                :key="status.id"
                                :command="[status]">{{status.name}}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <!-- 表格 -->
        <lin-table
                border
                :tableColumn="tableColumn"
                :tableData="tableData"
                :operate="operate"
                @handleEdit="handleEdit"
                @handleDelete="handleDelete"
                v-loading="loading"></lin-table>
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :background="true"
                    :page-size="pageCount"
                    :current-page="currentPage"
                    v-if="refreshPagination"
                    layout="total, prev, pager, next, jumper"
                    :total="total_nums"
            ></el-pagination>
        </div>
    </div>
    <!-- 添加页面 -->
    <city-add v-else-if="showAdd" @addClose="addClose" :provinceId="search.province_id" ></city-add>
    <!-- 修改页面 -->
    <city-edit v-else-if="showEdit" @editClose="editClose" :cityId="cityId" ></city-edit>
</template>

<script>
import province from '@/models/province'
import city from '@/models/city'
import LinSearch from '@/components/base/search/lin-search'
import LinTable from '@/components/base/table/lin-table'
import CityAdd from './CityAdd'
import CityEdit from './CityEdit'

export default {
  components: {
    CityAdd,
    LinSearch,
    LinTable,
    CityEdit,
  },
  data() {
    return {
      tableColumn: [
        { prop: 'name', label: '名称' },
        { prop: 'province.name', label: '省份' },
        { prop: 'update_time', label: '更新时间' },
      ],
      provinceArr: [],
      searchProvince: '',
      tableData: [],
      operate: [],
      loading: false,
      showAdd: false,
      showEdit: false,
      search: {
        name: '',
        province_id: 0,
      },
      cityId: 0,
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 10, // 每页10条数据
      total_nums: 0, // 分组内的总数
    }
  },
  computed: {},
  created() {
    // 获取省份
    this.getProvinceAll()
    this.loading = true
    // 获取数据
    this._getTableData(this.currentPage, this.pageCount)
    // 操作栏
    this.operate = [
      { name: '编辑', func: 'handleEdit', type: 'primary' },
      { name: '删除', func: 'handleDelete', type: 'danger' },
    ]
  },
  methods: {
    // 获取全部省份信息
    async getProvinceAll() {
      this.provinceArr = await province.getProvinceAll()
    },
    // 获取数据
    async _getTableData(start, count) {
      const res = await city.getCityList(start, count, { name: this.search.name, province_id: this.search.province_id })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
    handleAdd() {
      this.showAdd = true
    },
    rowClick(val) {
      console.log(val)
    },
    addClose() {
      this.showAdd = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    editClose() {
      this.showEdit = false
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    expandChange(row, expandedRows) {
      console.log(row, expandedRows)
    },
    handleCellSave(row) {
        row.editFlag = false; // eslint-disable-line
      setTimeout(() => {
        this.editRow--
        this.$message({
          type: 'success',
          message: '修改成功',
        })
      }, 1000)
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(this.currentPage, this.pageCount)
    },
    handleEdit(val) {
      this.showEdit = true
      this.cityId = Number(val.row.id)
    },
    handleDelete(val) {
      this
        .$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(async () => {
          this.loading = true
          const res = await city.deleteCity(val.row.id)
          if (res.error_code === 0) {
            this.$message.success(`${res.msg}`)
          }
          this._getTableData(this.currentPage, this.pageCount)
        })
    },
    // 搜索姓名
    onQueryChange(query) {
      this.search.name = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 切换省份
    // eslint-disable-next-line no-shadow
    handleCommand(province) {
      this.searchProvince = province[0]['name'] // eslint-disable-line
      // eslint-disable-next-line eqeqeq
      if (province[0] != '选择省份') {
        this.search.province_id = Number(province[0].id)
      } else {
        this.search.province_id = 0
      }
      this.currentPage = 1
      this.loading = true
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
}
</script>

<style lang="scss" scoped>
    .tableSample {
        padding: 0 0 30px;

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 30px;
            border-bottom: 1px solid #dae1ed;

            .header-left {
                float: left;

                .title {
                    height: 59px;
                    line-height: 59px;
                    color: $parent-title-color;
                    font-size: 16px;
                    font-weight: 500;
                }
            }

            .header-right {
                float: right;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
        }

        .table-main {
            padding: 0 30px;
            margin-top: 30px;
        }

        .top-operate {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            margin-top: 30px;
        }

        .sort-input {
            width: 50px;
            background: none;
            justify-content: space-around;
        }

        .table-edit {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .content {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .cell-icon {
                cursor: pointer;
                color: #3963bc;
                font-size: 16px;
            }

            .cell-icon-edit {
                display: flex;
                margin-left: 20px;
                width: 50px;
                justify-content: space-between;

                .cell-cancel {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                }

                .cell-save {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                    margin-right: -20px;
                }
            }
        }

        .m-20 {
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .summary {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;

            img {
                width: 135px;
                height: 200px;
            }

            .demo-table-expand {
                font-size: 0;
                margin-left: 30px;
                display: flex;
                flex-direction: column;

                label {
                    width: 90px;
                    color: #99a9bf;
                }

                .el-form-item {
                    margin-right: 0;
                    margin-bottom: 0;
                    display: flex;
                    flex-direction: row;
                }
            }
        }

        .pagination {
            display: flex;
            justify-content: flex-end;
            margin: 20px;
        }
    }
    // dialog
    .tableSample /deep/ .el-dialog__footer {
        text-align: left;
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__header {
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__body {
        padding: 30px;
    }
</style>
